# Support libraries for KiCad

_realname=kicad
pkgbase=mingw-w64-${_realname}-library
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-footprints"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-symbols"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-templates"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-meta")
pkgver=7.0.2
pkgrel=1
pkgdesc="Support libraries for KiCad (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url='https://www.kicad.org/'
license=("spdx:GPL-3.0-or-later")
groups=("${MINGW_PACKAGE_PREFIX}-eda")
depends=("${MINGW_PACKAGE_PREFIX}-${_realname}")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('!strip')
source=("https://gitlab.com/kicad/libraries/kicad-footprints/-/archive/${pkgver}/kicad-footprints-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-symbols/-/archive/${pkgver}/kicad-symbols-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-templates/-/archive/${pkgver}/kicad-templates-${pkgver}.tar.bz2"
        "https://gitlab.com/kicad/libraries/kicad-packages3D/-/archive/${pkgver}/kicad-packages3D-${pkgver}.tar.bz2")
sha256sums=('81ba4e1a48a4a741e3860d2e6b305a1002aea41c9ce168db13f9c7650198e374'
            'd0f9aed81172e14da899d90e2ead6ef8c4d515da3a3847a26bab22db4a7e4528'
            '2ca6de284aa6d1567173d3d5ef10bb7f416cc919b7a9cae438ebb36ced15df74'
            'a436414b9466db3aacfbe3efedfc784bcec2d2839789234fc65414069a9e470d')

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  for _s in "footprints" "symbols" "templates" "packages3D"; do
    msg2 "Build kicad-${_s}"
    [[ -d "${srcdir}"/build-${_s}-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${_s}-${MSYSTEM}
    mkdir -p "${srcdir}/build-${_s}-${MSYSTEM}" && cd "${srcdir}/build-${_s}-${MSYSTEM}"

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      "${MINGW_PREFIX}"/bin/cmake.exe \
        -GNinja \
        -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
        "${extra_config[@]}" \
        ../${_realname}-${_s}-${pkgver}

    "${MINGW_PREFIX}"/bin/cmake.exe --build .
  done
}

package_footprints() {
  pkgdesc="KiCad footprints library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-footprints-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-footprints-git)
  depends=()
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-footprints-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_symbols() {
  pkgdesc="KiCad symbols library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-symbols-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-symbols-git)
  depends=()
  optdepends=("${MINGW_PACKAGE_PREFIX}-${_realname}")

  cd "${srcdir}/build-symbols-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_templates() {
  pkgdesc="KiCad templates library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-templates-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-templates-git)

  cd "${srcdir}/build-templates-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_packages3D() {
  pkgdesc="KiCad 3D models library (mingw-w64)"
  conflicts=(${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D-git)
  replaces=(${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D-git)

  cd "${srcdir}/build-packages3D-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}

package_meta() {
  pkgdesc="Meta package for KiCad containing the core application and support libraries (mingw-w64)"

  depends=(${MINGW_PACKAGE_PREFIX}-${_realname}
           ${MINGW_PACKAGE_PREFIX}-${_realname}-footprints
           ${MINGW_PACKAGE_PREFIX}-${_realname}-symbols
           ${MINGW_PACKAGE_PREFIX}-${_realname}-templates
           ${MINGW_PACKAGE_PREFIX}-${_realname}-packages3D)
  optdepends=()
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-${_realname}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
